package com.sunlands.zlcx.usercenter.vo;

import com.sunlands.zlcx.usercenter.domain.InviteCodeDO;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shixiaopeng
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@DynamicUpdate
@DynamicInsert
public class InviteCodeVO implements Serializable {

    private static final long serialVersionUID = -1427975848079627189L;

    @ApiModelProperty(value = "用户id")
    private Long userId;

    @ApiModelProperty(value = "用户名称")
    private String userName;

    @ApiModelProperty(value = "用户头像")
    private String avatarUrl;

    @ApiModelProperty(value = "是否被使用，true已用，false没用")
    private Boolean isUsed;

    @ApiModelProperty(value = "被邀请到的用户id")
    private Long inviteUserId;

    @ApiModelProperty(value = "邀请码")
    private String code;

    @ApiModelProperty(value = "链接")
    private String link;


    public static InviteCodeVO fromDO(InviteCodeDO inviteCodeDO) {
        if (Objects.isNull(inviteCodeDO)) {
            return null;
        }
        InviteCodeVO inviteCodeVO = new InviteCodeVO();
        BeanUtils.copyProperties(inviteCodeDO, inviteCodeVO);

        return inviteCodeVO;
    }

    public static List<InviteCodeVO> fromDO(List<InviteCodeDO> inviteCodeDOList) {
        if (Objects.isNull(inviteCodeDOList)) {
            return Collections.emptyList();
        }

        return inviteCodeDOList.stream()
                .map(InviteCodeVO::fromDO)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}
